package cn.sollyu.SDManager;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.sollyu.SDManger.R;

public class DatabaseManagerService
{
	private Context context;

	// 要使用 DatabaseManagerService 必须传递一个上下文，来标识是谁在使用此类
	public DatabaseManagerService(Context context) {
		this.context = context;
	}
	
	protected String getMoreInfoString(String pString,String infoString,String defaultString) {
		String willReturnString = null;
		SQLiteDatabase databaseOpenHelper = new DatabaseOpenHelper(context).getReadableDatabase();
		String sqlString = String.format(context.getString(R.string.database_select_format),context.getString(R.string.database_table),context.getString(R.string.database_path),pString);
		Cursor cursor = databaseOpenHelper.rawQuery(sqlString,new String[] {});
		 if (cursor.moveToFirst())
		 {
			 willReturnString = (String) cursor.getString(cursor.getColumnIndex(infoString));
		 }else
		 {
			 willReturnString = defaultString;
		}

		databaseOpenHelper.close();
		return willReturnString;
	}
	
	public String getInfoString(String pathString) {
		return getMoreInfoString(pathString, context.getString(R.string.database_info), context.getString(R.string.database_info_not_fond));
	}
	
	public String getIndexString(String pathString) {
		return getMoreInfoString(pathString, context.getString(R.string.database_index),context.getString(R.string.database_index_not_fond));
	}
	
	public String getSoftString(String pathString) {
		return getMoreInfoString(pathString, context.getString(R.string.database_soft),context.getString(R.string.database_soft_not_fond));
	}
	
	public String getDelString(String pathString) {
		return getMoreInfoString(pathString, context.getString(R.string.database_del),context.getString(R.string.database_del_not_fond));
	}
	
	public String getUriString(String pathString) {
		return getMoreInfoString(pathString, context.getString(R.string.database_uri),context.getString(R.string.database_uri_not_fond));
	}
		
	public void addNewInfo(String pathString,String nameString, String infoString, String softString, String deleString,Integer indexInteger,String UriString) {
		SQLiteDatabase databaseOpenHelper = new DatabaseOpenHelper(context).getReadableDatabase();
		 
		@SuppressWarnings("static-access")
		String sql = String.format(context
				.getString(R.string.database_insert_format),
				context.getString(R.string.database_table),
				context.getString(R.string.database_path),
				context.getString(R.string.database_name),
				context.getString(R.string.database_info),
				context.getString(R.string.database_index),
				context.getString(R.string.database_soft),
				context.getString(R.string.database_del),
				context.getString(R.string.database_uri),
				pathString, nameString, infoString,
				new String().valueOf(indexInteger), softString,
				deleString,UriString);
//		Toast.makeText(context, sql, 1).show();
		
		databaseOpenHelper.execSQL(sql);
		databaseOpenHelper.close();
	}

	public void updateOneInfo(String pathString,String nameString, String infoString, String softString, String deleString,Integer indexInteger,String UriString) {
		SQLiteDatabase databaseOpenHelper = new DatabaseOpenHelper(context).getReadableDatabase();
		
		String sql = String.format(context.getString(R.string.database_update_format), 
				context.getString(R.string.database_table),
				context.getString(R.string.database_info),infoString,
				context.getString(R.string.database_index),indexInteger,
				context.getString(R.string.database_soft),softString,
				context.getString(R.string.database_del),deleString,
				context.getString(R.string.database_uri),UriString,
				context.getString(R.string.database_path),pathString);
		
//		Toast.makeText(context, sql, 1).show();
		databaseOpenHelper.execSQL(sql);
		databaseOpenHelper.close();
	}
}
